import React from 'react';
import { Route, Switch } from 'react-router-dom';
import routerConfig from './routerConfig';

function getRoutes(routes, basePath) {
    if (!Array.isArray(routes)) {
        return null;
    }
    const rs = routes.map((rt, i) => {
        const {children, path, component: Component, ...rest} = rt;
        let newPath = basePath + path;
        newPath = newPath.replace(/\/\//g, '/');
        return (
            <Route key={i} {...rest} 
                path={newPath}
                render={values => {
                return <Component {...values}>
                    {getRoutes(rt.children, newPath)}
                </Component>
            }} />
        )
    });
    return <Switch>
        {rs}
    </Switch>
}

export default function RootRouter() {
    return (
        <>
            {getRoutes(routerConfig, '')}
        </>
    )
}
